package com.feicheng.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.model.OrderMain;
import com.feicheng.persistence.model.VipMain;
import com.feicheng.persistence.service.*;
import com.feicheng.service.BaseService;
import com.feicheng.service.IFOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单业务逻辑
 * 
 * @see IFOrderService
 */
@Service
public class FOrderServiceImpl extends BaseService implements IFOrderService {
	@Autowired
	private IConsumeRecordService iConsumeRecordService;
	@Autowired
	private IOrderService iOrderService;
	@Autowired
	private IPaymentRecordService iPaymentRecordService;
	@Autowired
	private IOrderConsumeService iOrderConsumeService;
	@Autowired
	private IVipService iVipService;
	@Autowired
	private IPriceService iPriceService;
	@Autowired
	private IVoucherConsumeRecordService iVoucherConsumeRecordService;
	@Autowired
	private ICardConsumeRecordService iCardConsumeRecordService;
	@Autowired
	private ICardRecordService iCardRecordService;
	@Autowired
	private IAccountService iAccountService;
	@Autowired
	private IVoucherRecordService iVoucherRecordService;

	@Autowired
	IFOrderService ifOrderService;

	/**
	 * @see IFOrderService#getOrdersByPage(JSONObject)
	 */
	@Override
	public ResultInfo getOrdersByPage(JSONObject param) {
		// 创建条件构造器
		QueryWrapper<OrderMain> queryWrapper = new QueryWrapper<OrderMain>();
		queryWrapper.eq(Constant.ENTITY.ORDER.D_FLG, false);
		queryWrapper.orderByDesc(Constant.ENTITY.ORDER.CT);

		// 如果客户姓名不为空 加入检索条件
		if (null != param.getInteger(Constant.ENTITY.ORDER.STORE_ID)) {
			queryWrapper.eq(Constant.ENTITY.ORDER.STORE_ID, param.getInteger(Constant.ENTITY.ORDER.STORE_ID));
		}

		// 如果客户姓名不为空 加入检索条件
		if (null != param.getInteger(Constant.ENTITY.ORDER.CLIENT_ID)) {
			queryWrapper.eq(Constant.ENTITY.ORDER.CLIENT_ID, param.getInteger(Constant.ENTITY.ORDER.CLIENT_ID));
		}

		// 如果客户姓名不为空 加入检索条件
		if (StringUtils.hasLength(param.getString(Constant.ENTITY.ORDER.CLIENT_NAME))) {
			queryWrapper.like(Constant.ENTITY.ORDER.CLIENT_NAME, param.getString(Constant.ENTITY.ORDER.CLIENT_NAME));
		}
		// 如果微信订单编号部位空
		if (StringUtils.hasLength(param.getString(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE))) {
			queryWrapper.like(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE,
					param.getString(Constant.ENTITY.PAYMENT_RECORD.ORIGINAL_CODE));
		}
		// 如果订单支付类型不为空 加入检索条件
		if (StringUtils.hasLength(param.getString(Constant.ENTITY.CONSUME_RECORD.TYPE))) {
			queryWrapper.eq(Constant.ENTITY.CONSUME_RECORD.TYPE, param.getString(Constant.ENTITY.CONSUME_RECORD.TYPE));
		}
		// 如果订单状态不为空 加入检索条件
		if (StringUtils.hasLength(param.getString(Constant.ENTITY.ORDER.STATU))) {
			queryWrapper.eq(Constant.ENTITY.ORDER.STATU, param.getString(Constant.ENTITY.ORDER.STATU));
		}
		// 如果开始时间不为空且结束时间不为空 加入检索条件
		if (StringUtils.hasLength(param.getString(Constant.ENTITY.ORDER.ET))
				&& StringUtils.hasLength(param.getString(Constant.ENTITY.ORDER.ST))) {
			queryWrapper.ge(Constant.ENTITY.ORDER.ST, param.get(Constant.ENTITY.ORDER.ST));
			queryWrapper.le(Constant.ENTITY.ORDER.ET, param.get(Constant.ENTITY.ORDER.ET));
			// 如果结束时间不为空且开始时间为空 加入检索条件
		} else if (StringUtils.hasLength(param.getString(Constant.ENTITY.ORDER.ET))) {
			queryWrapper.le(Constant.ENTITY.ORDER.ET, param.get(Constant.ENTITY.ORDER.ET));
			// 如果开始时间不为空且结束时间为空 加入检索条件
		} else if (StringUtils.hasLength(param.getString(Constant.ENTITY.ORDER.ST))
				&& !StringUtils.hasLength(param.getString(Constant.ENTITY.ORDER.ET))) {
			queryWrapper.ge(Constant.ENTITY.ORDER.ST, param.get(Constant.ENTITY.ORDER.ST));
		}
		// 如果门店Id不为空 加入检索条件
		if (null != param.getInteger(Constant.ENTITY.ORDER.STORE_ID)) {
			queryWrapper.eq(Constant.ENTITY.ORDER.STORE_ID, param.getInteger(Constant.ENTITY.ORDER.STORE_ID));
		}

		// 如果手机号不为空 加入检索条件
		if (null != param.getString(Constant.ENTITY.ORDER.PHONE)) {
			queryWrapper.like(Constant.ENTITY.ORDER.PHONE, param.getString(Constant.ENTITY.ORDER.PHONE));
		}
		// 获得当前页
		Integer current = param.getInteger(Constant.HTTP.PARAM.CURRENT);
		// 获得每页条数
		Integer size = param.getInteger(Constant.HTTP.PARAM.SIZE);
		// 判断当前页是否为空,为空则默认第一页
		if (null == current) {
			current = 1;
		}
		// 判断条数是否为空，为空则默认一页十条
		if (null == size) {
			size = 10;
		}
		Page<OrderMain> orderMainPage = iOrderService.getOrdersByPage(new Page<OrderMain>(current, size), queryWrapper);
		return ResultInfo.success(orderMainPage);
	}

	/**
	 * @see IFOrderService#deleteOrder(Integer)
	 */
	@Override
	public ResultInfo deleteOrder(Integer orderId) {
		// 如果取得的参数为空 抛出异常
		if (null == orderId) {
			// throw new SystemException()
		}
		// 创建Order对象 对其orderId ut dFlg 进行赋值
		Order order = new Order();
		order.setOrderId(orderId);
		order.setUt(LocalDateTime.now());
		order.setDFlg(true);
		// 根据orderId删除订单
		QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
		orderQueryWrapper.eq(Constant.ENTITY.ORDER.ORDER_ID, orderId);
		iOrderService.update(order, orderQueryWrapper);
		return ResultInfo.success();
	}

	/**
	 * @see IFOrderService#updateOrder(JSONObject)
	 */
	@Override
	public ResultInfo updateOrder(JSONObject param) {
		// 如果取得的参数为空 抛出异常
		if (null == param.getObject(Constant.ENTITY.ORDER.TABLE, Order.class)) {
			// throw new SystemException()
		}
		// 取得参数order对象
		Order order = param.getObject(Constant.ENTITY.ORDER.TABLE, Order.class);
		// 对order对象的ut进行赋值
		order.setUt(LocalDateTime.now());
		// 根据OrderId更改订单信息
		QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
		orderQueryWrapper.eq(Constant.ENTITY.ORDER.ORDER_ID, order.getOrderId());
		iOrderService.update(order, orderQueryWrapper);
		return ResultInfo.success();
	}

	/**
	 * @see IFOrderService#getOrder(Integer)
	 */
	@Override
	public ResultInfo getOrder(Integer orderId) {

		// 如果取得的参数为空 抛出异常
		if (null == orderId) {
			// throw new SystemException()
		}

		// 创建Map集合
		JSONObject orderMap = new JSONObject();

		Order order = iOrderService.getById(orderId);
		if (order.getDFlg()) {
			throw new SystemException("该订单已删除");
		}
		// 将查询到的订单信息放入Map集合
		orderMap.put(Constant.ENTITY.ORDER.TABLE, order);

		// 创建条件构造器
		QueryWrapper<OrderConsume> orderConsumeQueryWrapper = new QueryWrapper<>();
		orderConsumeQueryWrapper.eq(Constant.ENTITY.ORDER.ORDER_ID, orderId);
		// 根据orderId查询订单消费记录
		List<OrderConsume> orderConsumes = iOrderConsumeService.list(orderConsumeQueryWrapper);

		if (null != orderConsumes && orderConsumes.size() > 0) {

			OrderConsume orderConsume = orderConsumes.remove(0);

			// 根据consumeRecordId查询消费记录
			ConsumeRecord consumeRecord = iConsumeRecordService.getById(orderConsume.getConsumeRecordId());
			// 将查询到的消费记录信息放入Map集合
			orderMap.put(Constant.ENTITY.CONSUME_RECORD.TABLE, consumeRecord);

			// 判断是否为支付
			if (null != consumeRecord.getPayId()) {
				// 创建条件构造器
				QueryWrapper<PaymentRecord> paymentRecordQueryWrapper = new QueryWrapper<>();
				paymentRecordQueryWrapper.eq(Constant.ENTITY.PAYMENT_RECORD.PAY_ID, consumeRecord.getPayId());
				// 根据payId查询支付记录
				List<PaymentRecord> paymentRecord = iPaymentRecordService.list(paymentRecordQueryWrapper);
				// 将查询到的支付记录信息放入Map集合
				orderMap.put(Constant.ENTITY.PAYMENT_RECORD.TABLE, paymentRecord);
			}
		}

		return ResultInfo.success(orderMap);
	}

	/**
	 * @see IFOrderService#createOrder(JSONObject)
	 */
	@Override
	public ResultInfo createOrder(JSONObject param) {
		// 如果取得的参数为空 抛出异常
		if (null == param.getObject(Constant.ENTITY.ORDER.TABLE, Order.class)) {
			// throw new SystemException()
		}
		Order order = param.getObject(Constant.ENTITY.ORDER.TABLE, Order.class);
		// 如果生成订单所需的参数 门店编号 门店名称 客户编号 客户名称为空 抛出异常
		if (null == order.getStoreId() || !StringUtils.hasLength(order.getStoreName()) || null == order.getClientId()
				|| !StringUtils.hasLength(order.getClientName())) {
			// throw new SystemException()
		}
		// 获取当前时间 对order对象的ct ut st et timeLongs 进行赋值
		LocalDateTime now = LocalDateTime.now();
		order.setCt(now);
		order.setUt(now);
		order.setSt(now);
		order.setEt(now);
		order.setTimeLongs(0);
		// 生成订单
		iOrderService.save(order);
		return ResultInfo.success();
	}

	/**
	 * @see com.feicheng.service.IFOrderService#consumeOrder(Integer)
	 */
	@Override
	@Transactional
	public ResultInfo consumeOrder(Integer orderId) {

		// 如果取得的参数为空 抛出异常
		if (null == orderId) {
			// throw new SystemException()
		}

		LocalDateTime et = LocalDateTime.now();

		// 创建条件构造器 取得每分钟的价钱
		QueryWrapper<Price> priceQueryWrapper = new QueryWrapper<>();
		priceQueryWrapper.isNull(Constant.ENTITY.PRICE.CARD_TYPE_ID);
		priceQueryWrapper.eq(Constant.ENTITY.PRICE.D_FLG, 0);
		priceQueryWrapper.eq(Constant.ENTITY.PRICE.STATU, 1);

		Integer price = iPriceService.list(priceQueryWrapper).get(0).getAmount();

		Order stOrder = iOrderService.getById(orderId);

		Integer clientId = stOrder.getClientId();

		// 创建条件构造器 根据clientId dFlg查询其所有订单记录
		QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
		orderQueryWrapper.eq(Constant.ENTITY.ORDER.CLIENT_ID, clientId);
		orderQueryWrapper.eq(Constant.ENTITY.ORDER.D_FLG, 0);

		List<Order> orders = iOrderService.list(orderQueryWrapper);

		boolean isConsume = false;

		// 判断是否为新客户 若查询到的订单记录仅有一条 并且订单状态为ST 则为新客户
		if (true) {
			// 计算非客户的锻炼时长
			Integer timeLongs = 0;
			if (0 == Duration.between(stOrder.getSt(), et).getSeconds() % 60) {
				timeLongs = (int) (Duration.between(stOrder.getSt(), et).getSeconds() / 60);
			} else {
				timeLongs = (int) (Duration.between(stOrder.getSt(), et).getSeconds() / 60) + 1;
			}
			// 如果订单时间小于一秒 会出现0元订单 使订单时间为一分钟
			if (0 == timeLongs) {
				timeLongs = 1;
			}

			// 对order对象的ut 结束时间 金额 锻炼时长 订单状态 进行赋值
			// 如果锻炼时长超过4h
			if (240 <= timeLongs) {
				stOrder.setUt(stOrder.getSt().plusHours(4));
				stOrder.setEt(stOrder.getSt().plusHours(4));
				stOrder.setTimeLongs(240);
				stOrder.setAmount(9600);
			} else {
				stOrder.setUt(et);
				stOrder.setEt(et);
				// 计算订单金额
				stOrder.setAmount(timeLongs * price);
				stOrder.setTimeLongs(timeLongs);
			}

			Integer amount = timeLongs * price;

			// 创建条件构造器 根据clientId statu 查询该客户是否有在使用的vip
			QueryWrapper<VipMain> vipMainQueryWrapper = new QueryWrapper<>();
			vipMainQueryWrapper.eq(Constant.ENTITY.VIP.STATU, 1);
			vipMainQueryWrapper.eq(Constant.ENTITY.CLIENT_VIP.CLIENT_ID, clientId);

			// 取得已使用的vip信息
			VipMain vipMains = iVipService.getVipByClientId(vipMainQueryWrapper).size() > 0
					? iVipService.getVipByClientId(vipMainQueryWrapper).get(0)
					: null;

			// 若取得的vip信息不为null 则该客户有在使用中的vip
			if (null != vipMains) {

				/* 有在使用中的vip 判断vip类别 如果cardRecordId不为空 则为会员卡类型 **/
				if (null != vipMains.getCardRecordId()) {

					/* 会员卡类型 对cardConsumeRecord对象的ct 金额 recordId 时长 进行赋值 */
					CardConsumeRecord cardConsumeRecord = new CardConsumeRecord();
					cardConsumeRecord.setCt(et);
					cardConsumeRecord.setAmount(amount);
					cardConsumeRecord.setRecordId(vipMains.getCardRecordId());
					cardConsumeRecord.setTimeLongs(timeLongs);

					// 添加会员卡消费记录数据
					iCardConsumeRecordService.save(cardConsumeRecord);

					// 根据询recordId查会员卡记录
					CardRecord cardRecord = iCardRecordService.getById(vipMains.getCardRecordId());

					// 对cardRecord对象的时长进行赋值
					cardRecord.setTimeLongs(cardRecord.getTimeLongs() - timeLongs);

					// 根据recordId对会员卡记录进行修改
					iCardRecordService.updateById(cardRecord);

					// 创建consumeRecord对象 对其种类 会员卡记录编号进行赋值
					ConsumeRecord consumeRecord = new ConsumeRecord();
					consumeRecord.setType(Constant.MODEL_NAME.Order.C);
					consumeRecord.setPayId(null);
					consumeRecord.setVCRecordId(null);
					consumeRecord.setCCRecordId(cardConsumeRecord.getCCRecordId());

					// 添加消费记录数据
					iConsumeRecordService.save(consumeRecord);

					// 创建orderConsume对象 对其订单编号 消费记录编号进行赋值
					OrderConsume orderConsume = new OrderConsume();
					orderConsume.setOrderId(stOrder.getOrderId());
					orderConsume.setConsumeRecordId(consumeRecord.getConsumeRecordId());

					// 添加订单消费数据
					iOrderConsumeService.save(orderConsume);

				} else {

					/*
					 * 会员券类型 对voucherConsumeRecord对象的ct 金额 recordId 时长 进行赋值
					 */
					VoucherConsumeRecord voucherConsumeRecord = new VoucherConsumeRecord();
					voucherConsumeRecord.setCt(et);
					voucherConsumeRecord.setAmount(amount);
					voucherConsumeRecord.setRecordId(vipMains.getVoucherRecordId());
					voucherConsumeRecord.setTimeLongs(timeLongs);

					// 添加会员券消费记录数据
					iVoucherConsumeRecordService.save(voucherConsumeRecord);

					// 根据recordId查询会员券记录
					VoucherRecord voucherRecord = iVoucherRecordService.getById(vipMains.getVoucherRecordId());

					// 对voucherRecord对象的时长进行赋值
					voucherRecord.setTimeLongs(voucherRecord.getTimeLongs() - timeLongs);

					// 根据recordId对会员券记录进行修改
					iVoucherRecordService.updateById(voucherRecord);

					// 创建consumeRecord对象 对其种类 会员券记录编号进行赋值
					ConsumeRecord consumeRecord = new ConsumeRecord();
					consumeRecord.setType(Constant.MODEL_NAME.Order.V);
					consumeRecord.setPayId(null);
					consumeRecord.setCCRecordId(null);
					consumeRecord.setVCRecordId(voucherConsumeRecord.getVCRecordId());

					// 添加消费记录数据
					iConsumeRecordService.save(consumeRecord);

					// 创建orderConsume对象 对其订单编号 消费记录编号进行赋值
					OrderConsume orderConsume = new OrderConsume();
					orderConsume.setOrderId(stOrder.getOrderId());
					orderConsume.setConsumeRecordId(consumeRecord.getConsumeRecordId());

					// 添加订单消费数据
					iOrderConsumeService.save(orderConsume);
				}

				// 根据clientId查询账户
				QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
				accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID, clientId);
				Account account = iAccountService.list(accountQueryWrapper).get(0);

				// subTime为会员截止时间-出门时间
				long subTime = (Duration.between(et, vipMains.getEt()).getSeconds());

				// 判断vip是否到期 若subTime>0 则vip没到期
				if (subTime > 0) {

					/*
					 * vip没到期 对account对象的时长进行赋值
					 */
					account.setTimeLongs(account.getTimeLongs() - timeLongs);

					// 根据clientId更改账户
					iAccountService.updateById(account);

				} else {

					/*
					 * vip到期 对account对象的时长进行赋值
					 */
					account.setTimeLongs(0);

					// 根据clientId更改账户
					iAccountService.updateById(account);

					// 根据vipId 修改过期vip的状态
					Vip oldVip = iVipService.getById(vipMains.getVipId());
					oldVip.setStatu(2);

					iVipService.updateById(oldVip);
				}

				// 对order对象的订单状态进行赋值
				stOrder.setStatu(Constant.MODEL_NAME.Order.FIN);

				// 对订单进行修改
				iOrderService.updateById(stOrder);
				isConsume = true;

			} else {

				/*
				 * 没有在使用中的vip 根据clientId statu 查询该客户是否有未使用的vip 通过ct进行升序排序
				 */
				QueryWrapper<VipMain> unUse = new QueryWrapper<>();
				unUse.eq(Constant.ENTITY.VIP.STATU, 0);
				unUse.eq(Constant.ENTITY.CLIENT_VIP.CLIENT_ID, clientId);
				unUse.orderByAsc(Constant.ENTITY.VIP.CT);

				List<VipMain> unUseVip = iVipService.getVipByClientId(unUse);

				// 如果取得的unUseVip长度大于0 则进行开卡或者开券
				if (0 < unUseVip.size()) {

					// 根据clientId查询账户
					QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
					accountQueryWrapper.eq(Constant.ENTITY.ACCOUNT.CLIENT_ID, clientId);

					Account account = iAccountService.list(accountQueryWrapper).get(0);

					// 如果取得cardRecordId不为null 则为会员卡
					if (null != unUseVip.get(0).getCardRecordId()) {
						CardRecord cardRecord = iCardRecordService.getById(unUseVip.get(0).getCardRecordId());

						// 对cardRecord对象的st et statu 进行赋值
						cardRecord.setSt(stOrder.getSt());
						cardRecord.setEt(stOrder.getSt().plusMinutes(cardRecord.getTimeLongs()));
						cardRecord.setStatu(false);

						// 根据vipId取得vip信息
						Vip vip = iVipService.getById(unUseVip.get(0).getVipId());

						// 对vip对象的st et statu进行赋值
						vip.setSt(stOrder.getSt());
						vip.setEt(stOrder.getSt().plusMinutes(cardRecord.getTimeLongs()));
						vip.setStatu(1);

						// 对cardRecord对象的timeLongs进行赋值
						cardRecord.setTimeLongs(cardRecord.getTimeLongs() - timeLongs);
						iCardRecordService.updateById(cardRecord);
						iVipService.updateById(vip);

						// 对cardConsumeRecord对象的ct 金额 recordId 时长 进行赋值
						CardConsumeRecord cardConsumeRecord = new CardConsumeRecord();
						cardConsumeRecord.setCt(et);
						cardConsumeRecord.setAmount(amount);
						cardConsumeRecord.setRecordId(cardRecord.getRecordId());
						cardConsumeRecord.setTimeLongs(timeLongs);

						// 添加会员卡消费记录数据
						iCardConsumeRecordService.save(cardConsumeRecord);

						// 创建consumeRecord对象 对其种类 会员卡记录编号进行赋值
						ConsumeRecord consumeRecord = new ConsumeRecord();
						consumeRecord.setType(Constant.MODEL_NAME.Order.C);
						consumeRecord.setPayId(null);
						consumeRecord.setVCRecordId(null);
						consumeRecord.setCCRecordId(cardConsumeRecord.getCCRecordId());

						// 添加消费记录数据
						iConsumeRecordService.save(consumeRecord);

						// 创建orderConsume对象 对其订单编号 消费记录编号进行赋值
						OrderConsume orderConsume = new OrderConsume();
						orderConsume.setOrderId(stOrder.getOrderId());
						orderConsume.setConsumeRecordId(consumeRecord.getConsumeRecordId());

						// 添加订单消费数据
						iOrderConsumeService.save(orderConsume);

						// 对account对象的时长进行赋值
						account.setTimeLongs(cardRecord.getTimeLongs());
						// 根据clientId更改账户
						iAccountService.updateById(account);

						// 对order对象的statu进行赋值
						stOrder.setStatu(Constant.MODEL_NAME.Order.FIN);

						// 对订单进行修改
						iOrderService.updateById(stOrder);
						isConsume = true;
					} else {

						// 如果取得cardRecordId为null 则为会员券
						VoucherRecord voucherRecord = iVoucherRecordService
								.getById(unUseVip.get(0).getVoucherRecordId());

						// 对voucherRecord对象的st et 进行赋值
						voucherRecord.setSt(stOrder.getSt());
						voucherRecord.setEt(stOrder.getSt().plusMinutes(voucherRecord.getTimeLongs()));

						// 根据vipId取得vip信息
						Vip vip = iVipService.getById(unUseVip.get(0).getVipId());

						// 对vip对象的st et statu进行赋值
						vip.setSt(stOrder.getSt());
						vip.setEt(stOrder.getSt().plusMinutes(voucherRecord.getTimeLongs()));
						vip.setStatu(1);

						// 对voucherRecord对象的timeLongs进行赋值
						voucherRecord.setTimeLongs(voucherRecord.getTimeLongs() - timeLongs);

						iVoucherRecordService.updateById(voucherRecord);

						iVipService.updateById(vip);

						// 对voucherConsumeRecord对象的ct 金额 recordId 时长 进行赋值
						VoucherConsumeRecord voucherConsumeRecord = new VoucherConsumeRecord();
						voucherConsumeRecord.setCt(et);
						voucherConsumeRecord.setAmount(amount);
						voucherConsumeRecord.setRecordId(voucherRecord.getRecordId());
						voucherConsumeRecord.setTimeLongs(timeLongs);

						// 添加会员卡消费记录数据
						iVoucherConsumeRecordService.save(voucherConsumeRecord);

						// 创建consumeRecord对象 对其种类 会员券记录编号进行赋值
						ConsumeRecord consumeRecord = new ConsumeRecord();
						consumeRecord.setType(Constant.MODEL_NAME.Order.V);
						consumeRecord.setPayId(null);
						consumeRecord.setCCRecordId(null);
						consumeRecord.setVCRecordId(voucherConsumeRecord.getVCRecordId());

						// 添加消费记录数据
						iConsumeRecordService.save(consumeRecord);

						// 创建orderConsume对象 对其订单编号 消费记录编号进行赋值
						OrderConsume orderConsume = new OrderConsume();
						orderConsume.setOrderId(stOrder.getOrderId());
						orderConsume.setConsumeRecordId(consumeRecord.getConsumeRecordId());

						// 添加订单消费数据
						iOrderConsumeService.save(orderConsume);

						// 对account对象的时长进行赋值
						account.setTimeLongs(voucherRecord.getTimeLongs());

						// 根据clientId更改账户
						iAccountService.updateById(account);

						// 对order对象的statu进行赋值
						stOrder.setStatu(Constant.MODEL_NAME.Order.FIN);

						// 对订单进行修改
						iOrderService.updateById(stOrder);
						isConsume = true;
					}
				} else {
					// 非会员结算
					stOrder.setStatu(Constant.MODEL_NAME.Order.UNPAY);

					// 对订单进行修改
					iOrderService.updateById(stOrder);

				}
			}

		}

		if (!isConsume) {

			// 判断是否为新客户 若查询到的订单记录仅有一条 并且订单状态为ST 则为新客户
			if (1 == orders.size() && Constant.MODEL_NAME.Order.ST.equals(orders.get(0).getStatu())) {

				// 计算新客户的锻炼时长
				Integer timeLongs = 0;
				if (0 == Duration.between(orders.get(0).getSt(), et).getSeconds() % 60) {
					timeLongs = (int) (Duration.between(orders.get(0).getSt(), et).getSeconds() / 60);
				} else {
					timeLongs = (int) (Duration.between(orders.get(0).getSt(), et).getSeconds() / 60) + 1;
				}
				// 如果订单时间小于一秒 会出现0元订单 使订单时间为一分钟
				if (0 == timeLongs) {
					timeLongs = 1;
				}

				// 对order对象的ut 结束时间 金额 锻炼时长 订单状态 进行赋值
				Order order = orders.get(0);

				// 如果锻炼时长超过4h
				if (240 <= timeLongs) {
					order.setUt(stOrder.getSt().plusHours(4));
					order.setEt(stOrder.getSt().plusHours(4));
					order.setTimeLongs(240);
				} else {
					order.setUt(et);
					order.setEt(et);
					order.setTimeLongs(timeLongs);
				}
				order.setAmount(0);
				order.setStatu(Constant.MODEL_NAME.Order.FIN);

				// 对新客户的订单进行修改
				iOrderService.updateById(order);

			}

		}

		return ResultInfo.success();
	}

	public void consumeTooLongOrder() {
		QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
		LocalDateTime time = LocalDateTime.now().minusHours(24);
		orderQueryWrapper.lt(Constant.ENTITY.ORDER.ST, time);
		orderQueryWrapper.eq(Constant.ENTITY.ORDER.STATU, Constant.MODEL_NAME.Order.ST);
		List<Order> tooLongOrder = iOrderService.list(orderQueryWrapper);
		for (Order order : tooLongOrder) {
			ifOrderService.consumeOrder(order.getOrderId());
		}
	}
}
